//
//  HomeVM.m
//  ZhiHuDaily
//
//  Created by 吴宜昊 on 2022/2/5.
//

#import "HomePageVM.h"
#import "StorageManager.h"

@interface HomePageVM ()

@end
@implementation HomePageVM


#pragma mark - lazy
-(NSArray<HomeBannerM *> *) arrayBanner {
    if (!_arrayBanner) {
        _arrayBanner = [NSArray array];
    }
    return _arrayBanner;
}

-(NSMutableArray *) arrayDate {
    if (!_arrayDate) {
        _arrayDate = [NSMutableArray array];
    }
    return _arrayDate;
}

-(NSMutableArray<NSArray *> *)arrayList {
    if (!_arrayList) {
        _arrayList = [NSMutableArray array];
    }
    return _arrayList;
}

#pragma mark - 获取当日最新新闻
- (void) requestLatest {
    WeakSelf(self);
    [NetworkManager GET:HttpLatestNewsURL parameters:nil success:^(id  _Nonnull responseObject) {
        StrongSelf(weakSelf);
        [strongSelf.bindVC endRefresh];
        NSString * strDate = responseObject[@"date"];
        if (strDate && strDate.isNotEmpty) {
            strongSelf.arrayDate = @[strDate].mutableCopy;
        }
        
        NSArray * arrayStories = responseObject[@"stories"];
        if (arrayStories && arrayStories.isNotEmpty) {
            NSArray * arrayLatest = [NSArray yy_modelArrayWithClass:HomePageM.class json:arrayStories];
            strongSelf.arrayList = @[arrayLatest].mutableCopy;
        }
        
        NSArray * arrayTop_stories = responseObject[@"top_stories"];
        if (arrayStories && arrayStories.isNotEmpty) {
            strongSelf.arrayBanner = [NSArray yy_modelArrayWithClass:HomeBannerM.class json:arrayTop_stories];
        }
        
        [strongSelf reloadTableView];
        [strongSelf updateBanner];
        [strongSelf updateNavBar];
    } failure:^(NSError * _Nonnull error) {
        StrongSelf(weakSelf);
        [strongSelf.bindVC endRefresh];
    }];
}

- (void) requestBefore {
    if (self.arrayDate.isEmpty) {
        return;
    }
    NSString * dataString = self.arrayDate.lastObject;
    NSString * strURL = [NSString stringWithFormat:@"%@/%@",HttpBeforeNewsURL,dataString];
    NSDictionary * historyResponse = [self readFromStorage:dataString];
    if (historyResponse) {
        NSLog(@"YH:取得缓存数据 %@",dataString);
        [self showMoreData:historyResponse];
        return;
    }
    
    WeakSelf(self);
    [NetworkManager GET:strURL parameters:nil success:^(id  _Nonnull responseObject) {
        StrongSelf(weakSelf);
        NSLog(@"YH:取得http数据 %@",dataString);
        [StorageManager.shared save:responseObject fileName:dataString];
        [strongSelf showMoreData:responseObject];
    } failure:^(NSError * _Nonnull error) {
        StrongSelf(weakSelf);
        [strongSelf.bindVC endRefresh];
    }];
}

-(NSDictionary *) readFromStorage:(NSString *) strURL {
    NSDictionary * historyDictionary = [StorageManager.shared readWithFileName:strURL];
    return historyDictionary;
}

-(void) showMoreData:(NSDictionary *) response {
    [self.bindVC endRefresh];
    NSString * strDate = response[@"date"];
    if (strDate && strDate.isNotEmpty) {
        [self.arrayDate addObject:strDate];
    }
    
    NSArray * arrayStories = response[@"stories"];
    if (arrayStories && arrayStories.isNotEmpty) {
        NSArray * arrayBefore = [NSArray yy_modelArrayWithClass:HomePageM.class json:arrayStories];
        [self.arrayList addObject:arrayBefore];
    }
    [self reloadTableView];
}

#pragma mark - update
/// 更新最新新闻
-(void) reloadTableView {
    [self.bindVC reloadTableView];
}

/// 更新最新Banner
-(void) updateBanner {
    if (self.arrayBanner.isEmpty) {
        return;
    }
    
    NSMutableArray * mUrls = [NSMutableArray array];
    NSMutableArray * mTitles = [NSMutableArray array];
    for (int i = 0; i < self.arrayBanner.count; i++ ) {
        HomeBannerM * model = self.arrayBanner[i];
        [mUrls addObject:model.image];
        [mTitles addObject:model.title];
    }
    
    [self.bindVC reloadBanner:mUrls titles:mTitles];
}

/// 更新 navBar 日期
-(void) updateNavBar {
    if (self.arrayDate.isEmpty) {
        return;
    }
    DateModel * model = [DateModel modelWithString:self.arrayDate.firstObject];
    [self.bindVC updateDate:model];
}

@end
